from utils.goods import get_categories
import time
# from ..contents.models import ContentCategory
from meiduo_mall import settings
import os


def generic_meiduo_index():

    # 查询数据
    categories = get_categories()
    # 2.广告数据
    contents = {}

    from contents.models import ContentCategory
    content_categories = ContentCategory.objects.all()
    for cat in content_categories:
        contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

    # 我们的首页 后边会讲解页面静态化
    # 我们把数据 传递 给 模板
    context = {
        'categories': categories,
        'contents': contents,
    }

    # 页面静态化：渲染
    # 1、加载渲染模板
    from django.template import loader
    # index_template = loader.get_template('index.html')
    index_template = loader.get_template('index.html')
    # 2、把数据传给模板
    index_html_data = index_template.render(context)
    # 3、把渲染好的HTML，写入到指定文件

    # base_dir 的上一级
    file_path = os.path.join(os.path.dirname(settings.BASE_DIR), 'front_end_pc/index.html')

    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(index_html_data)



